from random import randint
from time import time
from abc import abstractclassmethod, ABC


class Sort(ABC):
    '''排序基类'''

    def __init__(self, lt=True):
        '''排序条件
        lt True:按从小到大排序;True:按从大到小排序'''
        self.lt = lt
        self.exchange_counter = 0
        self.compare_counter=0

    def name(self):
        return "排序"
    def compare(self, s, t, strict=True):
        '''比较'''
        if strict:
            return s < t if self.lt else s > t
        return s <= t if self.lt else s >= t

    @staticmethod
    def show(data, line_width=10, promp="data:"):
        '''打印排序结果'''
        print(f"{promp}")
        for i, item in enumerate(data):
            if i % line_width == 0:
                print("\n")
            print(f" {item}", end=" ")
        print("")

    @staticmethod
    def generate_data(count=30, low=0, hight=100):
        '''生成测试数据'''
        return [randint(low, hight) for i in range(count)]

    @abstractclassmethod
    def sort(self, data, low=0, hight=None):
        '''排序，需要子类复写'''
        pass

    def statistics(self, data):
        '''执行排序，并统计排序时间'''
        start = time()
        self.sort(data)
        return time()-start

    def exchange(self, data, i, j):
        '''交换指定位置数据'''
        tem = data[i]
        data[i] = data[j]
        data[j] = tem
        self.exchange_counter += 1

    def is_satisfy(self, data):
        '''判断数据是否排序完成'''
        result = True
        for i in range(0, len(data)):
            if i > 0 and self.compare(data[i], data[i-1], True):
                result = False
                break
        return result
